package com.ruicar.afs.cloud.apply.pre.loan.mq.receiver;

import cn.hutool.core.util.ObjectUtil;
import com.ruicar.afs.cloud.apply.common.entity.ApplyRemindDetails;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyRemindDetailsService;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ApproveInformDto;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.ApproveSubmitInfo;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.LeaveMessageDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.parameter.commom.enums.WhetherEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 耿继辉
 * 追加留言接收及保存MQ
 */
@AllArgsConstructor
@Slf4j
@Component
@ConditionalOnProperty(prefix = "com.ruicar.afs.cloud.mq.rabbit", name = "enable")
public class ApplyLeaveMessageDealProcessor implements AfsMqBizProcessor<ApproveSubmitInfo> {
    private final ApplyRemindDetailsService applyRemindDetailsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean processMessage(ApproveSubmitInfo entity) throws Exception {
        if (ObjectUtil.isNotNull(entity.getLeaveMessageDto())) {
            this.saveRemind(entity.getLeaveMessageDto());
        } else {
            log.info("解析失败,{}", entity);
        }
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_DEAL_WITH_MESSAGE;
    }

    @Override
    public boolean reQueueOnFail() {
        return false;
    }

    /**
     * 保存追加留言
     *
     * @param entity
     */
    public void saveRemind(LeaveMessageDto entity) {
        //解析保存追加留言
        ApplyRemindDetails applyRemindDetails = new ApplyRemindDetails();
        applyRemindDetails.setApplyNo(entity.getApplyNo());//申请编号
        applyRemindDetails.setIsReturn(entity.getIsRturn());//
        applyRemindDetails.setIsSelf(WhetherEnum.No.getIndex());//
        applyRemindDetails.setRemindType(entity.getMessageType());//留言类型
        applyRemindDetails.setCreateBy("审批专员");//创建者
        applyRemindDetails.setMsgContent(entity.getMessageInfo());//留言内容
        applyRemindDetails.setCreateTime(new Date());//时间
        applyRemindDetails.setMessageReason(entity.getMessageReason());
        this.applyRemindDetailsService.save(applyRemindDetails);
        log.info("信审追加留言保存成功{}", applyRemindDetails);
    }
}
